//富民手动退款记录
app.controller('handrefund', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', '$timeout', function ($scope, $rootScope, commonFactory, toaster, $modal, $timeout, ) {
    $scope.refundData = {//查询条件
        UserId: '',
        UserName: '',
        UserType:'3',
        Mobile: '',
        Index:1,
        Size:10
    }
    //查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.refundData.Index = 1;
        requestRefundtList();
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.refundData.Index >= $scope.page) {//判断最大页码
                $scope.refundData.Index = $scope.page;
                return false;
            }
            $scope.refundData.Index++
            requestRefundtList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.refundData.Index <= 1) {//判断最小页码
            $scope.refundData.Index = 1;
            return false;
        }
        $scope.refundData.Index--;
        requestRefundtList()
    }
    $scope.amountPaid = 0 //退款金额合计
    $scope.handamount = 0 //未退款金额
    $scope.totalMoney = 0 //已退款金额
    var requestRefundtList = function () {
        commonFactory.loading(true)
        if ($scope.refundData.UserId == undefined) {
            $scope.refundData.UserId = ''
        }
        if ($scope.refundData.UserName == undefined) {
            $scope.refundData.UserName = ''
        }
        if ($scope.refundData.Mobile == undefined) {
            $scope.refundData.Mobile = ''
        }
        commonFactory.ajax(commonFactory.baseurl + "/api/Move/GetMoveList", $scope.refundData, function (result) {
            $scope.amountPaid = 0;
            $scope.handamount = 0;
            commonFactory.loading(false)
            $scope.graybtn = false;
            if(result.Code ==1) {
                if (result.List) {
                    $scope.getRefundtList = result.List;
                    $scope.getRefundtTotal = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.refundData.Size);
                }
            }else {
                toaster.pop('warning', '失败', '获取数据失败')
            }
        }, function () {
            $scope.graybtn = false;
            commonFactory.loading(false)
        }, "post");
         commonFactory.ajax(commonFactory.baseurl + "/api/Move/GetMoveTotal", $scope.refundData, function (result) {
            $scope.amountPaid = 0;
            $scope.handamount = 0;
            commonFactory.loading(false)
            $scope.graybtn = false;
            if(result.Code ==1) {
                $scope.TotalHAmount = result.Data.TotalHAmount;
                $scope.TotalWAmount = result.Data.TotalWAmount;
                $scope.totalMoney = $scope.TotalHAmount + $scope.TotalWAmount;
            }else {
                toaster.pop('warning', '失败', '获取总计失败')
            }
        }, function () {
            $scope.graybtn = false;
            commonFactory.loading(false)
        }, "post");
    }
    requestRefundtList()
    $scope.refund = function (mid) {
        $scope.items = mid
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.refundshow = true;
                $scope.ok = function () {
                    commonFactory.loading(true)
                    commonFactory.ajax(commonFactory.baseurl + '/api/Move/SaveMove', { '': mid }, function (result) {
                        commonFactory.loading(false)
                        if (result.Code == 1) {
                            toaster.pop('success', '成功', '完成退款成功')
                            requestRefundtList()
                        }else if(result.Data){
                            toaster.pop('error', '失败', result.Data)
                        }else {
                            toaster.pop('error', '失败', '完成退款失败')
                        }
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '系统异常')
                    }, 'post')
                    $modalInstance.close('cancel');
                }
                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: 'sm',
            backdrop: false
        });
        // modalInstance.result.then(function (selectedItem) {
        //     alert(1)
        //     requestRefundtList()
        // }, function () {

        // });
    }
    //导出
    $scope.export = function () {
        if ($scope.refundData.UserId == undefined) {
            $scope.refundData.UserId = ''
        }
        if ($scope.refundData.UserName == undefined) {
            $scope.refundData.UserName = ''
        }
        if ($scope.refundData.Mobile == undefined) {
            $scope.refundData.Mobile = ''
        }
        var uId = localStorage.getItem('u_id')||'';
        $scope.gourl = commonFactory.baseurl + '/api/Move/Expoit?UserId=' + $scope.refundData.UserId + '&UserName=' + $scope.refundData.UserName + '&UserType=' + $scope.refundData.UserType + '&Mobile=' + $scope.refundData.Mobile + '&Size=2000' + '&Index=' + $scope.refundData.Index + '&u_Id=' + uId;
    }
}]);
// 借款人还款金额计划
app.controller('expectrepayment', ['$scope', 'commonFactory', 'toaster', function ($scope, commonFactory, toaster) {
    //明天的时间
    var day3 = new Date();
    day3.setTime(day3.getTime()+24*60*60*1000);
    var tomorrow = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
    $scope.repaymentData = {
        DtStart:'2018-02-03',
        DtEnd:tomorrow
    }
    var requesrepayment = function () {
        if(!$scope.repaymentData.DtEnd){
            toaster.pop('warning', '警告', '请选择开始日期');
            $scope.graybtn = false;
            return;
        }else if(new Date($scope.repaymentData.DtEnd).getTime() < new Date('2018/02/04').getTime()){
            toaster.pop('warning', '警告', '结束日期必须大于开始日期');
            $scope.graybtn = false;
            return;
        }
        $scope.repaymentData.DtEnd = commonFactory.DateFormat($scope.repaymentData.DtEnd, 1);
        commonFactory.loading(true)
        commonFactory.ajax("/api/Other/GetEstimateRepayment", $scope.repaymentData, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if (result.Code == 1) {
                $scope.repaymentList = JSON.parse(result.ResposeData);
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requesrepayment()
    $scope.query = function(){
        $scope.graybtn = true;
        requesrepayment()
    }
   
}]);
//红包发放情况
app.controller('redpacket', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', '$timeout', function ($scope, $rootScope, commonFactory, toaster, $modal, $timeout) {
    var today = new Date()
    $scope.RedAndEGList = {
        time: today
    }
    var RedAndEGList = function () {
        if ($scope.RedAndEGList.time) {
            $scope.RedAndEGList.time = commonFactory.DateFormat($scope.RedAndEGList.time, 1)
        }
        commonFactory.loading(true)
        commonFactory.ajax(commonFactory.baseurlstatistic + "/Other/RedAndEGList", { Dt: $scope.RedAndEGList.time }, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                if (result.RedAndEGInfoList != null) {
                    result.RedAndEGInfoList.forEach(function (element) {
                        var strdate = element.FinishTime.replace("/Date(", "").replace(")/", "");
                        element.FinishTime = commonFactory.DateFormat(new Date(parseInt(strdate)))
                    });
                    $scope.RedAndEGListone = result.RedAndEGInfoList;
                }
            } else {
                toaster.pop('error', '失败', result.ResposeData)
            }
        }, function () {
            commonFactory.loading(false)
        }, "post");
        commonFactory.ajax(commonFactory.baseurlstatistic + "/Other/RedStatistic", { Dt: $scope.RedAndEGList.time }, function (result) {
            if (result.Code == 1) {
                $scope.RedStatistic = result.RedStatisticInfo
            } else {
                toaster.pop('error', '失败', result.ResposeData)
            }
        }, function () {
        }, "post");
        $timeout(function () {
            $scope.graybtn = false;
        }, 1000);
    }
    $scope.redsearch = function () {
        $scope.graybtn = true;
        RedAndEGList()
    }
    RedAndEGList()
}]);
//体验金发放情况
app.controller('bbin', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
    commonFactory.ajax(commonFactory.baseurlstatistic + "/UserGiftCards/GetGiftCardStatistics", {}, function (result) {
        $scope.GetGiftCardStatistics = result;
    }, function () {

    }, "post");
    commonFactory.ajax(commonFactory.baseurlstatistic + "/UserGiftCards/MaxGiftPrice", {}, function (result) {
        $scope.maxGiftPrice = result;
    }, function () {

    }, "post");
    commonFactory.ajax(commonFactory.baseurlstatistic + "/UserGiftCards/GetUserGiftCard", {}, function (result) {
        result.forEach(function (list) {
            list.Time = list.Time.replace("/Date(", "").replace(")/", "");
            list.List.forEach(function (element) {
                element.CreateTime = element.CreateTime.replace("/Date(", "").replace(")/", "");
                element.ExpireTime = element.ExpireTime.replace("/Date(", "").replace(")/", "");
            });
        });
        $scope.GetExtractUser = result;
    }, function () {

    }, "post");
}]);
//还款管理
app.controller('Repaymentmanagement', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', '$timeout', function ($scope, $rootScope, commonFactory, toaster, $modal, $timeout, ) {
    $scope.index = 1;
    $scope.size = 10;
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.index >= $scope.page) {//判断最大页码
                $scope.index = $scope.page;
                return false;
            }
            $scope.index++
            requestProductList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.index <= 1) {//判断最小页码
            $scope.index = 1;
            return false;
        }
        $scope.index--;
        requestProductList()
    }
    $scope.returndata = {//查询条件
        ProductGroup: '0',
        ProductCode: '',
        FirstOnlineTime: '',
        FullStartTime: '',
        FullEndTime: '',
        LoanUserId: '',
        ProductName: '',
        PageIndex: $scope.index,
        PageSize: $scope.size
    }
    // ProductGroup: $scope.returndata.ProductGroup || '', 
    // ProductCode: $scope.returndata.ProductCode || '', 
    // FirstOnlineTime: $scope.returndata.FirstOnlineTime || '',
    // FullEndTime: $scope.returndata.FullEndTime || '', 
    // FullStartTime: $scope.returndata.FullStartTime || '', 
    // LoanUserId: $scope.returndata.LoanUserId || '', 
    // PageIndex: $scope.index, 
    // PageSize: $scope.size
    //查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.index = 1 //查询后在第一页显示
        if ($scope.returndata.FirstOnlineTime) {
            $scope.returndata.FirstOnlineTime = commonFactory.DateFormat($scope.returndata.FirstOnlineTime, 1);
        }else{
            $scope.returndata.FirstOnlineTime=''
        }
        if ($scope.returndata.FullStartTime) {
            $scope.returndata.FullStartTime = commonFactory.DateFormat($scope.returndata.FullStartTime, 1);
        }else{
            $scope.returndata.FullStartTime=''
        }
        if ($scope.returndata.FullEndTime) {
            $scope.returndata.FullEndTime = commonFactory.DateFormat($scope.returndata.FullEndTime, 1);
        }else{
            $scope.returndata.FullEndTime=''
        }
        requestProductList();
    }

    var requestProductList = function () {
        commonFactory.loading(true)
        commonFactory.ajax(commonFactory.baseurlstatistic + "/RepaymentMoney/GetRepaymentList", $scope.returndata, function (result) {
            commonFactory.loading(false)
            if (result.List) {
                result.List.forEach(function (element) {
                    element.CreateTime = element.CreateTime.replace("/Date(", "").replace(")/", "");
                    element.ExpireTime = element.ExpireTime.replace("/Date(", "").replace(")/", "");
                    element.FirstOnlineTime = element.FirstOnlineTime.replace("/Date(", "").replace(")/", "");
                    element.FullTime = element.FullTime.replace("/Date(", "").replace(")/", "");
                });
                $scope.GetRepaymentList = result.List;
                $scope.GetRepaymentListTotal = result.Total;
                $scope.page = Math.ceil(result.Total / $scope.size);
            } else {
                toaster.pop('warning', '', '返回其他code')
            }
        }, function () {
            commonFactory.loading(false)
        }, "post");
        commonFactory.ajax(commonFactory.baseurlstatistic + "/RepaymentMoney/GetTotalRepayment", $scope.returndata, function (result) {
            if (result.RepaymentTotal) {
                $scope.RepaymentTotal = result.RepaymentTotal
            } else {
                toaster.pop('warning', '', '返回其他code')
            }
        }, function () {

        }, "post");
        $timeout(function () {
            $scope.graybtn = false;
        }, 1000);
    }
    requestProductList()
    $scope.repaymentinfo = function (pid) {

        $scope.items = pid
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', 'items', function ($scope, $modalInstance, toaster, commonFactory, $timeout, items) {
                $scope.repaymentinfoshow = true;
                commonFactory.loading(true)
                commonFactory.ajax(commonFactory.baseurlstatistic + '/Other/ProductRepayment', { ProductId: items }, function (result) {
                    commonFactory.loading(false)
                    if (result.Code == 1) {
                        result.ProductRepaymentInfos.forEach(function (element) {
                            element.RepayDate = element.RepayDate.replace("/Date(", "").replace(")/", "");
                        });
                        $scope.repaymentInfo = result.ProductRepaymentInfos;
                    }
                }, function () {
                    commonFactory.loading(false)
                }, 'post')
                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
                $scope.exportdetail = function () {
                    $scope.gourl = 'http://192.168.0.109:9012/Other/ExcelProductRepayment?ProductId=' + items

                }
            }],
            size: 'lg',
            backdrop: false,
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) { }, function () { });
    }
    //导出
    $scope.export = function () {
        if ($scope.returndata.FirstOnlineTime == undefined || $scope.returndata.FirstOnlineTime == '') {
            $scope.returndata.FirstOnlineTime = '1970-01-01'
        } else {
            $scope.returndata.FirstOnlineTime = commonFactory.DateFormat($scope.returndata.FirstOnlineTime, 1);
        }
        if ($scope.returndata.FullEndTime == undefined || $scope.returndata.FullEndTime == "") {
            $scope.returndata.FullEndTime = '1970-01-01'
        } else {
            $scope.returndata.FullEndTime = commonFactory.DateFormat($scope.returndata.FullEndTime, 1)
        }
        if ($scope.returndata.FullStartTime == undefined || $scope.returndata.FullStartTime == "") {
            $scope.returndata.FullStartTime = '1970-01-01'
        } else {
            $scope.returndata.FullStartTime = commonFactory.DateFormat($scope.returndata.FullStartTime, 1)
        }
        if ($scope.returndata.ProductCode == undefined || $scope.returndata.ProductCode == '') {
            $scope.returndata.ProductCode = ''
        }
        if ($scope.returndata.ProductName == undefined || $scope.returndata.ProductName == '') {
            $scope.returndata.ProductName = ''
        }
        if ($scope.returndata.LoanUserId == undefined || $scope.returndata.LoanUserId == '') {
            $scope.returndata.LoanUserId = ''
        }
        if ($scope.returndata.ProductGroup == undefined || $scope.returndata.ProductGroup == '') {
            $scope.returndata.ProductGroup = ''
        }
        $scope.gourl = commonFactory.baseurlstatistic + '/RepaymentMoney/Expoit?FirstOnlineTime=' + $scope.returndata.FirstOnlineTime + '&FullEndTime=' + $scope.returndata.FullEndTime + '&FullStartTime=' + $scope.returndata.FullStartTime + '&ProductCode=' + $scope.returndata.ProductCode + '&ProductName=' + $scope.returndata.ProductName + '&ProductGroup=' + $scope.returndata.ProductGroup + '&LoanUserId=' + $scope.returndata.LoanUserId + '&PageIndex=' + $scope.index + '&PageIndex=' + $scope.size
    }
}]);
//活期产品统计
app.controller('currentstatistic', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$interval', '$modal', function ($scope, $http, commonFactory, toaster, $timeout, $interval, $modal) {
    var initdata = function () {
        $scope.index = 1;
        $scope.size = 15;
        $scope.godata = function () {//页面size
            requestProductList()
        }
        //下一页
        $scope.next = function () {
            if ($scope.page) {
                if ($scope.index >= $scope.page) {//判断最大页码
                    $scope.index = $scope.page;
                    return false;
                }
                $scope.index++
                requestProductList();
            }
        }
        //end
        $scope.end = function () {
            if ($scope.page) {
                $scope.index = $scope.page;
                requestProductList();
            }
        }
        //上一页
        $scope.prev = function () {
            if ($scope.index <= 1) {//判断最小页码
                $scope.index = 1;
                return false;
            }
            $scope.index--;
            requestProductList()
        }
        $scope.requestArr = {//查询条件
            ProductName: '',
            ProductCode: '',
            ProductGroup: '',
            UserId: '',
            ProductStatus: '',
            FullstartTime: '',
            FullendTime: '',
            IsSq: '',
            pageIndex: $scope.index, 
            pageSize: $scope.size
        }
        //查询
        $scope.search = function () {
            $scope.index = 1 //查询后在第一页显示
            if($scope.requestArr.FullstartTime){
                $scope.requestArr.FullstartTime=commonFactory.DateFormat($scope.requestArr.FullstartTime, 1)
            }else{
                $scope.requestArr.FullstartTime=''//处理时间为undefined情况
            }
            if($scope.requestArr.FullendTime){
                $scope.requestArr.FullendTime=commonFactory.DateFormat($scope.requestArr.FullendTime, 1)
            }else{
                $scope.requestArr.FullendTime=''
            }
            $scope.graybtn = true;
            requestProductList();
        }
        var requestProductList = function () {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Flexible/QueryFlexibleStatics", $scope.requestArr, function (result) {
                commonFactory.loading(false)
                $scope.graybtn = false;
                if (result.Code == 1) {
                    $scope.QueryLoanProductList = result.RealyList;
                    $scope.TotalLoanPrice = result.TotalLoanPrice;
                    $scope.TotalFullPrice = result.TotalFullPrice;
                    $scope.QueryLoanProductListTotal = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.size);
                    $scope.status = function (sta) {
                        switch (sta) {
                            case 1: return '未发布';
                            case 2: return '已发布';
                            case 3: return '募资中';
                            case 4: return '已满标';
                            case 5: return '还款中';
                            case 6: return '提前还清';
                            case 7: return '已还清';
                            case 8: return '等待推送至银行';
                            case 13: return '流标';
                            case 15: return '草稿';
                            case 16: return '已删除';
                        }
                    }

                } else {
                    toaster.pop('error', '失败', result.ResposeData);
                }
            }, function () {
                commonFactory.loading(false)
                $scope.graybtn = false;
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
        requestProductList();
    }
    initdata();
}]);
//过渡期还款统计
app.controller('transition', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', '$timeout', function ($scope, $rootScope, commonFactory, toaster, $modal, $timeout, ) {
    $scope.index = 1;
    $scope.size = 10;
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.index >= $scope.page) {//判断最大页码
                $scope.index = $scope.page;
                return false;
            }
            $scope.index++
            requestProductList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.index <= 1) {//判断最小页码
            $scope.index = 1;
            return false;
        }
        $scope.index--;
        requestProductList()
    }
    $scope.returndata = {//查询条件
        ProductGroup: '0',
        ProductCode: '',
        FirstOnlineTime: '',
        FullStartTime: '',
        FullEndTime: '',
        LoanUserId: '',
        ProductName: '',
        PageIndex: $scope.index,
        PageSize: $scope.size
    }
    //查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.index = 1 //查询后在第一页显示
        if ($scope.returndata.FirstOnlineTime) {
            $scope.returndata.FirstOnlineTime = commonFactory.DateFormat($scope.returndata.FirstOnlineTime, 1);
        }else{
            $scope.returndata.FirstOnlineTime=''
        }
        if ($scope.returndata.FullStartTime) {
            $scope.returndata.FullStartTime = commonFactory.DateFormat($scope.returndata.FullStartTime, 1);
        }else{
            $scope.returndata.FullStartTime=''
        }
        if ($scope.returndata.FullEndTime) {
            $scope.returndata.FullEndTime = commonFactory.DateFormat($scope.returndata.FullEndTime, 1);
        }else{
            $scope.returndata.FullEndTime=''
        }
        requestProductList();
    }

    var requestProductList = function () {
        commonFactory.loading(true)
        commonFactory.ajax(commonFactory.baseurlstatistic + "/RepaymentMoney/GetRepaymentList", $scope.returndata, function (result) {
            commonFactory.loading(false)
            if (result.List) {
                result.List.forEach(function (element) {
                    element.CreateTime = element.CreateTime.replace("/Date(", "").replace(")/", "");
                    element.ExpireTime = element.ExpireTime.replace("/Date(", "").replace(")/", "");
                    element.FirstOnlineTime = element.FirstOnlineTime.replace("/Date(", "").replace(")/", "");
                    element.FullTime = element.FullTime.replace("/Date(", "").replace(")/", "");
                });
                $scope.GetRepaymentList = result.List;
                $scope.GetRepaymentListTotal = result.Total;
                $scope.page = Math.ceil(result.Total / $scope.size);
            } else {
                toaster.pop('warning', '', '返回其他code')
            }
        }, function () {
            commonFactory.loading(false)
        }, "post");
        $timeout(function () {
            $scope.graybtn = false;
        }, 1000);
    }
    requestProductList()

}]);